/**
 * 统一管理
 *  1开始 重新开始 结束逻辑
 *  2 敌人单位创建
 */
var global = require("Global")
var dao = require("DataDao")
cc.Class({
    extends: cc.Component,

    properties: {

        createY : { // 下一个敌人出生的Y轴位置
            default:0,
            type: cc.Float,
        },
        target: {//相机组件CameraMove
            default: null,
            type: cc.Node
        },
        player: {//玩家对象
            default: null,
            type: cc.Node
        },
        camera: {//相机 和target 同一个单位
            default: null,
            type: cc.Node
        },
        gameState://0 暂停 1 游戏中 2 结束 
            {
                default: 0,
                type: cc.Integer
            },

        uiGameMain: //主ui
            {
                default: null,
                type: cc.Node
            },

        speedTime://玩了多长时间
            {
                default:0.0,
                type:cc.Float
            },
        enemyNode: //node
            {
                default: null,
                type: cc.Node
            },
        global:null,
        gold:{
            default:0.0,
            type:cc.Float
        },
    },

    isGamePause()
    {
        return this.gameState == 0;
    },
    isGaming()
    {
        return this.gameState == 1;
    },
    isGameOver()
    {
        return this.gameState == 2;
    },
    isPassLevel()
    {
        return this.gameState == 3;
    },


    // onLoad () {},

    main()
    {
        this.startGame() ;
    },

    startGame () {
        this.gold = 0.0;
        this.gameState = 1;
        this.target.getComponent("CameraMove").enabled  = true;
        this.player.getComponent("HeroControl").enabled  = true;
        this.uiGameMain.getComponent("UiGameMain").onShow();
        this.uiGameMain.getComponent("UiGameMain").hideSkillIntroduce();
        this.player.getComponent("HeroControl").init();
        this.completeFlag = true;

    },
    continueGame()
    {
        this.gameState = 1;
        this.target.getComponent("CameraMove").enabled  = true;
        this.player.getComponent("HeroControl").enabled  = true;
        this.uiGameMain.getComponent("UiGameMain").onShow();
    },
    reStartGame()//重新开始
    {
        cc.director.loadScene("platform");
    },
    nextLevelGame()
    {
        dao.netLevel();
        //保存
        cc.director.loadScene("platform");

    },

    gameOver ()
    {
        if(this.gameState != 2)//
        {
            var gold = Math.floor(this.gold);
            dao.addGold(gold);//添加金币
            this.uiGameMain.getComponent("UiGameMain").gameOverPopTipToCurr(dao.getGoldLabel(gold));

        }
        this.gameState = 2;
        this.target.getComponent("CameraMove").enabled  = false;
        this.player.getComponent("HeroControl").enabled  = false;
    },
    passLevel()
    {

        if(this.gameState==1){
            /**
             * 计算本关得分
             */
            var level = dao.getCurrentLevel();
            var index = Math.floor( level/10);
            var gold = global.getGoldConfig()[index].extra+this.gold;
            gold = Math.floor(gold);

            dao.addGold(gold)//添加金币产出
            this.uiGameMain.getComponent("UiGameMain").gameOverPopTipToNext(dao.getGoldLabel(gold));
        }


        this.gameState = 3;
        this.target.getComponent("CameraMove").enabled  = false;
        this.player.getComponent("HeroControl").enabled  = false;

        //this.uiGameMain.getComponent("UiGameMain").onShow();

    },

    GetScore()
    {
        return this.gold ;
    },

    initUiPop(){
        var bgRank = cc.find('Canvas/Main Camera/uiMain/RankPop');
        bgRank.active = false;
        var bgSetup = cc.find('Canvas/Main Camera/uiMain/SetupPop');
        bgSetup.active = false;
    },

    onLoad: function () {//初始函数
        this.gameState = 0;
        this.target.getComponent("CameraMove").enabled  = false;
        //this.player.getComponent("HeroControl").enabled  = false;


        global.curLevelConfig = null;//初始化关卡配置
        global.initLevelConfig();
        this.global = global;
        this.level = dao.getCurrentLevel();
        this.uiGameMain.getComponent("UiGameMain").onShow();
        this.initUiPop();
    },
    randomSwap()//随机出生敌人
    {
        if(this.target.y  > this.createY)//刷新单位 根据相机位置刷新野怪
        {
            var posY = this.createY + 1200;
            var posX = Math.random() * 640;

            var distance =300 - Math.floor(this.speedTime ) /15.0;//刷新间隔
            if(distance < 10)
                distance =10;


            this.createY += distance;

            var tName= Math.min(Math.floor(Math.random()*7+0.1),7);

            var self = this;
            var resPath ="";
            if(tName<1.1) //随机刷新敌方单位
            {
                resPath = "enemy1";
            }else if(tName<2.1)
            {
                resPath = "enemy2";
            }else if( tName < 3.1)
            {
                resPath = "enemy3";
            }else if( tName < 4.1)
            {
                resPath = "enemy4";
            }else if( tName < 5.1)
            {
                resPath = "enemy5";
            }else if( tName < 6.1)
            {
                resPath = "enemy6";
            }else if( tName < 7.1)
            {
                resPath = "enemy7";
            }else if( tName < 8.1)
            {
                resPath = "enemy8";
            }else if( tName < 9.1)
            {
                resPath = "enemy9";
            }else if( tName < 10.1)
            {
                resPath = "enemy10";
            }


            // resPath = "enemy7";

            cc.loader.loadRes(resPath, function (err, prefab) {

                var newNode = cc.instantiate(prefab);
                cc.director.getScene().addChild(newNode);
                newNode.active = true;
                newNode.scale = 0.7+ Math.random() ;
                if(newNode ==null)
                    return;
                newNode.y = posY;
                newNode.x = posX;
                newNode.getComponent("ActorConfig").Random();//数据初始化
                newNode.getComponent("ActorConfig").setNode(self.camera);
            });
        }
    },
    configSwap()//走配置出现
    {

        var configSwap = global.curLevelConfig;
        //console.log("++++++++global.curLevelConfig"+configSwap.map[0].x);


        var curSwapEnemy =  configSwap[global.curEnemySwapIndex];


        if(curSwapEnemy == null )
        {

            this.passLevel();
            return;
        }



        if(this.target.y > curSwapEnemy.y)
        {
            var yy = this.target.y;
            var self = this;
            var resPath = "";
            {
                var ememyTempId = curSwapEnemy.enemyId;
                var tName = 1;
              
                if(typeof ememyTempId == typeof [])
                {
                    var allNum = ememyTempId.length  ;
                    tName = Math.min(Math.floor(Math.random()*allNum+0.1),allNum-1);
                    tName = ememyTempId[tName];
  
                }else if (ememyTempId == -1)
                {
                    tName = Math.min(Math.floor(Math.random()*7+0.1),7);
                }else
                {
                    tName = ememyTempId;
                }

                if(tName<1.1) //随机刷新敌方单位
                {
                    resPath = "enemy1";
                }else if(tName<2.1)
                {
                    resPath = "enemy2";
                }else if( tName < 3.1)
                {
                    resPath = "enemy3";
                }else if( tName < 4.1)
                {
                    resPath = "enemy4";
                }else if( tName < 5.1)
                {
                    resPath = "enemy5";
                }else if( tName < 6.1)
                {
                    resPath = "enemy6";
                }else if( tName < 7.1)
                {
                    resPath = "enemy7";
                }else if( tName < 8.1)
                {
                    resPath = "enemy8";
                }else if( tName < 9.1)
                {
                    resPath = "enemy9";
                }else if( tName < 10.1)
                {
                    resPath = "enemy10";
                }

            }





            cc.loader.loadRes(resPath, function (err, prefab) {

                var newNode = cc.instantiate(prefab);
                cc.director.getScene().addChild(newNode);

                //   newNode.scale = curSwapEnemy.scale ;


                newNode.parent = self.node;
                newNode.getComponent("ActorConfig").initData(
                    curSwapEnemy.scale ,curSwapEnemy.x ,curSwapEnemy.y +1200,
                    curSwapEnemy.weight,curSwapEnemy.speed,curSwapEnemy.dirx,curSwapEnemy.trail);//数据初始化
                newNode.getComponent("ActorConfig").setNode(self.camera);

            
        
                var heroSkill = dao.getHeroSkill();    
                var skillLevel = heroSkill[1];            
                var skillCfg = global.getSkillUp(1);
                newNode.scale = newNode.scale* skillCfg.bufferEffect(skillCfg,skillLevel);

        

            });

            global.curEnemySwapIndex += 1; //下一个
        }
    },
    update (dt) {
        if(this.gameState != 1)
            return;
        this.speedTime += dt;
        if(this.level){
            var index = Math.floor( this.level/10)
            this.gold = global.getGoldConfig()[index].time*this.speedTime;//时间增长金币
        }
        if(global.curMode == 2)//随机模式 ==1
        {
            this. configSwap();
            this. configSwap();
            this. configSwap();
            this. configSwap();
        }else
        {
            this.randomSwap();
            //  randomSwap();
        }
    },

});